//
//  RBBrandSubVC.m
//  zhongzhenglu
//
//  Created by mac on 2022/12/15.
//

#import "RBBrandSubVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import "RBSearchTagView.h"
#import "RBThemeConfig.h"
#import <Masonry.h>
#import "RBNavigationBar.h"
#import "RBTextField.h"
#import "UIButton+RBAdd.h"
#import <UIColor+YYAdd.h>
#import "UIViewController+RBAdd.h"
#import "RBPager.h"
#import "RanTool.h"
#import "RBBrandResponse.h"
#import "RBBrandSubCell.h"
#import "RBEmptyView.h"
#import "RBCollectionReusableView.h"
#import "RBBrandGoodsListVC.h"

@interface RBBrandSubVC ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property(nonatomic, strong) RBBrandResponse *response;

@property(nonatomic, strong) UICollectionView *collectionView;
@property(nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation RBBrandSubVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self getData];
}

#pragma mark - Private

- (void)setup
{
    self.view.backgroundColor = [UIColor colorWithHexString:@"#BDE3F8"];
    self.collectionView.hidden = NO;
}

- (void)fillData
{
    __weak typeof(self) weak_self = self;
    [self.dataSource removeAllObjects];
    CGFloat headerH = Zoom(50);
    CGFloat emptyH = Zoom(100);
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        group.headerReuseIdentifier = NSStringFromClass(RBCollectionReusableView.class);
        group.headerReferenceSize = CGSizeMake(kRBMainScreenWidth, headerH);
        group.headerView = [self headerViewWithTitle:@"知名品牌"];
        group.minimumLineSpacing = Zoom(13);
        group.cellModels = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            for (RBBrandSubModel *model in self.response.zhiming)
            {
                [arrayM addObject:({
                    RBCellModel *cellModel = RBCellModel.new;
                    cellModel.reuseCellIdentifier = NSStringFromClass(RBBrandSubCell.class);
                    cellModel.cellData = model;
                    cellModel.itemSize = CGSizeMake(Zoom(356), Zoom(151));
                    model.displayType = arrayM.count%4;
                    cellModel.cellSelectBlock = ^(NSIndexPath *indexPath) {
                        RBBrandGoodsListVC *vc = RBBrandGoodsListVC.new;
                        RBBrandSubModel *model = [RanTool modelWithSection:indexPath.section row:indexPath.row dataSource:weak_self.dataSource];
                        vc.brand = model.brand_name;
                        vc.id = model.id;
                        [weak_self.navigationController pushViewController:vc animated:YES];
                    };
                    cellModel;
                })];
            }
            arrayM;
        });
        if(!group.cellModels.count)
        {
            group.footerReuseIdentifier = NSStringFromClass(RBCollectionReusableView.class);
            group.footerReferenceSize = CGSizeMake(kRBMainScreenWidth, emptyH);
            group.footerView = RBEmptyView.new;
        }
        group;
    })];
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        group.headerReuseIdentifier = NSStringFromClass(RBCollectionReusableView.class);
        group.headerReferenceSize = CGSizeMake(kRBMainScreenWidth, headerH);
        group.headerView = [self headerViewWithTitle:@"自主品牌"];
        group.minimumLineSpacing = Zoom(13);
        group.cellModels = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            for (RBBrandSubModel *model in self.response.zizhu)
            {
                [arrayM addObject:({
                    RBCellModel *cellModel = RBCellModel.new;
                    cellModel.reuseCellIdentifier = NSStringFromClass(RBBrandSubCell.class);
                    cellModel.cellData = model;
                    cellModel.itemSize = CGSizeMake(Zoom(356), Zoom(151));
                    model.displayType = arrayM.count%4;
                    cellModel.cellSelectBlock = ^(NSIndexPath *indexPath) {
                        RBBrandGoodsListVC *vc = RBBrandGoodsListVC.new;
                        RBBrandSubModel *model = [RanTool modelWithSection:indexPath.section row:indexPath.row dataSource:weak_self.dataSource];
                        vc.brand = model.brand_name;
                        vc.id = model.id;
                        [weak_self.navigationController pushViewController:vc animated:YES];
                    };
                    cellModel;
                })];
            }
            arrayM;
        });
        if(!group.cellModels.count)
        {
            group.footerReuseIdentifier = NSStringFromClass(RBCollectionReusableView.class);
            group.footerReferenceSize = CGSizeMake(kRBMainScreenWidth, emptyH);
            group.footerView = RBEmptyView.new;
        }
        group;
    })];
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        group.headerReuseIdentifier = NSStringFromClass(RBCollectionReusableView.class);
        group.headerReferenceSize = CGSizeMake(kRBMainScreenWidth, headerH);
        group.headerView = [self headerViewWithTitle:@"推荐品牌"];
        group.minimumLineSpacing = Zoom(13);
        group.sectionInset = UIEdgeInsetsMake(0, 0, Zoom(40), 0);
        group.cellModels = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            for (RBBrandSubModel *model in self.response.index)
            {
                [arrayM addObject:({
                    RBCellModel *cellModel = RBCellModel.new;
                    cellModel.reuseCellIdentifier = NSStringFromClass(RBBrandSubCell.class);
                    cellModel.cellData = model;
                    cellModel.itemSize = CGSizeMake(Zoom(356), Zoom(151));
                    model.displayType = arrayM.count%4;
                    cellModel.cellSelectBlock = ^(NSIndexPath *indexPath) {
                        RBBrandGoodsListVC *vc = RBBrandGoodsListVC.new;
                        RBBrandSubModel *model = [RanTool modelWithSection:indexPath.section row:indexPath.row dataSource:weak_self.dataSource];
                        vc.brand = model.brand_name;
                        vc.id = model.id;
                        [weak_self.navigationController pushViewController:vc animated:YES];
                    };
                    cellModel;
                })];
            }
            arrayM;
        });
        if(!group.cellModels.count)
        {
            group.footerReuseIdentifier = NSStringFromClass(RBCollectionReusableView.class);
            group.footerReferenceSize = CGSizeMake(kRBMainScreenWidth, emptyH);
            group.footerView = RBEmptyView.new;
        }
        group;
    })];
    [self.collectionView reloadData];
}

#pragma mark - Network

- (void)getData
{
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"brand_classid"] = self.id;
    } path:@"han_brand_index" post:YES log:YES responseClass:RBBrandResponse.class desView:self.view showHud:YES showPlaceHolder:YES sucBlock:^(id data) {
        weak_self.response = data;
        [weak_self fillData];
    } failBlock:nil reloadBlock:^{
        [weak_self getData];
    }];
}

#pragma mark - Event

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

kRBCollectionViewDataSourceAndDelegate(self.dataSource);

#pragma mark - Setter

#pragma mark - Getter

- (UIView *)headerViewWithTitle: (NSString *)title
{
    UIView *view = UIView.new;
    [view h_view_addView:[RanTool labelWithFontSize:Zoom(15) weight:1 color:[RBThemeConfig textBlackColor] align:0 text:title] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(10) right:0 centerY:0];
    return view;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        self.collectionView = ({
            UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new;
            //layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize;
            UICollectionView *view = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout];
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            view.dataSource = self;
            view.delegate = self;
            view.backgroundColor = UIColor.clearColor;
            kRBIgnoreScrollViewInsetsAdjust(view);
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.right.mas_equalTo(0);
                make.bottom.mas_equalTo(0);
                make.top.mas_equalTo(0);
            }];
            view;
        });
    }
    return _collectionView;
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource)
    {
        self.dataSource = ({
            NSMutableArray *arrayM = [NSMutableArray array];
            arrayM;
        });
    }
    return _dataSource;
}

@end
